'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 战斗结束
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var arenaBattleRecordModel = require('../../../../models/arenaBattleRecordModel.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var battle_list = [];
    async.waterfall(
        [
            function (cb) {
                arenaBattleRecordModel.findByRoleId(role_id, cb);
            },
            function (rpt_list, cb) {
                _.forEach(rpt_list, function (rpt) {
                    var battle_info = {};

                    battle_info.record_id = rpt.battle_id;
                    battle_info.battle_result = rpt.battle_result;
                    battle_info.rank_changed = rpt.rank_changed;
                    battle_info.create_tm = rpt.create_tm;

                    var rival_info;
                    if (rpt.atk_role_id === role_id) {
                        // 进攻
                        rival_info = rpt.def;
                        battle_info.rival_id = rpt.def_role_id;
                        battle_info.side = 1;
                    } else {
                        // 防守
                        rival_info = rpt.atk;
                        battle_info.rival_id = rpt.atk_role_id;
                        battle_info.side = 2;

                        battle_info.rank_changed = -battle_info.rank_changed;
                        battle_info.battle_result = -battle_info.battle_result;
                    }
                    battle_info.name = rival_info.name;
                    battle_info.job_type = rival_info.job_type;
                    battle_info.lv = rival_info.lv;
                    battle_info.vip = rival_info.vip;
                    battle_info.union_name = rival_info.union_name;
                    battle_info.union_flag = rival_info.union_flag;
                    battle_info.rank = rival_info.rank;
                    battle_info.fight_power = rival_info.fight_power;

                    battle_list.push(battle_info);
                });

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    battle_list: battle_list
                }
            });
        }
    );
};
